﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PM.Web.Admin
{
    public partial class admin_website_i_addup : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["action"]))
                {
                    if (Request.QueryString["action"] == "del")
                    {
                        delData();
                    }
                }
                bindComp();
                bindBigClass();
                bindSmallClass();
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    this.hidId.Value = Request.QueryString["id"];
                    bindData();
                }

            }
        }

        private void bindComp()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlCommunity.DataSource = dal.GetCommunity(int.Parse(Session["CommId"].ToString()), true);
            this.ddlCommunity.DataBind();
        }

        private void bindBigClass()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlBigClass.DataSource = dal.GetWebSiteIBigClassList(int.Parse(this.ddlCommunity.SelectedValue), null);
            this.ddlBigClass.DataBind();
        }

        private void bindSmallClass()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.ddlSmallClass.DataSource = dal.GetWebSiteISmallClassList(int.Parse(this.ddlCommunity.SelectedValue), int.Parse(this.ddlBigClass.SelectedValue), null);
            this.ddlSmallClass.DataBind();
        }

        protected void ddlCommunity_SelectIndexChanged(object sender, EventArgs e)
        {
            bindBigClass();
        }

        protected void ddlBigClass_SelectIndexChanged(object sender, EventArgs e)
        {
            bindSmallClass();
        }

        private void delData()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                if (dal.DelWebSiteI(int.Parse(Request.QueryString["id"])))
                {
                    Response.Write("ok");
                }
                else
                {
                    Response.Write("err");
                }
            }
            Response.End();
        }

        private void bindData()
        {
            if (this.hidId.Value != "0")
            {
                DataAccess.PmData dal = new DataAccess.PmData();
                System.Data.DataTable dt = dal.GetWebSiteI(int.Parse(this.hidId.Value));
                if (dt.Rows.Count > 0)
                {
                    this.ddlCommunity.Items.FindByValue(dt.Rows[0]["self_community_code"].ToString()).Selected = true;
                    this.ddlCommunity.Enabled = false;
                    this.ddlBigClass.Items.FindByValue(dt.Rows[0]["bigclassid"].ToString()).Selected = true;
                    this.ddlSmallClass.Items.FindByValue(dt.Rows[0]["smallclassid"].ToString()).Selected = true;
                    this.txtTitle.Text = dt.Rows[0]["title"].ToString();
                    this.editor_id.Value = dt.Rows[0]["content"].ToString();
                    this.radCanSignY.Checked = Convert.ToBoolean(dt.Rows[0]["cansign"]);
                    this.radCanSignN.Checked = !Convert.ToBoolean(dt.Rows[0]["cansign"]);

                    bindSubItem();
                }
            }
        }

        private void bindSubItem()
        {
            DataAccess.PmData dal = new DataAccess.PmData();
            this.dgdSubItem.DataSource = dal.GetWebSiteISubItemList(int.Parse(this.hidId.Value), null);
            this.dgdSubItem.DataBind();
        }


        private bool SaveData()
        {
            string sCommunity = this.ddlCommunity.SelectedValue;
            string sBigClassid = this.ddlBigClass.SelectedValue;
            string sSmallClassid = this.ddlSmallClass.SelectedValue;

            string sTitle = this.txtTitle.Text.Trim();
            string sContent = this.editor_id.Value;

            int iCanSign = this.radCanSignY.Checked ? 1 : 0;
            if (sTitle == string.Empty)
            {
                PM.Web.Common.JsAlert(this, "标题不能为空！");
                return false;
            }

            DataAccess.PmData dal = new DataAccess.PmData();
            if (hidId.Value == "0")
            {
                int newId = 0;
                if ((newId = dal.AddWebSiteI(int.Parse(sBigClassid), int.Parse(sSmallClassid), sTitle, sContent, iCanSign)) > 0)
                {
                    this.hidId.Value = newId.ToString();
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return dal.ModifyWebSiteI(int.Parse(hidId.Value), int.Parse(sBigClassid), int.Parse(sSmallClassid), sTitle, sContent, iCanSign);
            }
            return true;
        }

        protected void btnSubSave_Click(object sender, EventArgs e)
        {
            if (SaveData())
            {
                string sSubTitle = this.txtSubTitle.Text.Trim();
                DataAccess.PmData dal = new DataAccess.PmData();
                dal.AddWebSiteISubItem(int.Parse(this.hidId.Value), sSubTitle, string.Empty);
                bindSubItem();
            }
        }

        protected void cmdOk_Click(object sender, EventArgs e)
        {
            if (SaveData())
            {
                PM.Web.Common.JsAlert(this, "保存成功！", true);
            }
        }

        protected void dgdSubItem_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            string sCommandName = e.CommandName;
            int id = Convert.ToInt32(dgdSubItem.DataKeys[e.Item.ItemIndex]);
            DataAccess.PmData dal = new DataAccess.PmData();
            switch (sCommandName)
            {
                case "Edit":
                    this.dgdSubItem.EditItemIndex = e.Item.ItemIndex;
                    bindSubItem();
                    break;
                case "Update":
                    string sSubTitle = ((TextBox)e.Item.FindControl("txtEditSubTitle")).Text.Trim();
                    if (sSubTitle == string.Empty)
                    {
                        PM.Web.Common.JsAlert(this, "细项内容不能为空！");
                        return;
                    }
                    dal.ModifyWebSiteISubItem(id, sSubTitle, string.Empty);
                    this.dgdSubItem.EditItemIndex = -1;
                    bindSubItem();
                    break;
                case "Delete":
                    dal.DelWebSiteISubItem(id);
                    bindSubItem();
                    break;
                case "Cancel":
                    this.dgdSubItem.EditItemIndex = -1;
                    bindSubItem();
                    break;
            }
        }
    }
}